﻿using Newtonsoft.Json;
using System.IO;
using System.Runtime.InteropServices;

namespace iTool_Cetector
{
    public class GlobalUtils
    {

        static string _PublicIp { get; set; }
        static string _GatewayUrl { get; set; }
        static string _SeedString { get; set; }

        const string _SeedfileName = "seed.itool";
        static string _iToolConfigurePath { get; } = "/var/iTool/Release.iToolConfig";


        static GlobalUtils() 
        {
            string currentDirectory = string.Empty;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                currentDirectory = $"D:/iTool-Cloud-Framework";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                currentDirectory = "/var/iTool";
            }
            bool isDebug = false;
#if DEBUG
            isDebug = true;
            _GatewayUrl = "http://localhost:2994";
#endif
            _iToolConfigurePath = $"{currentDirectory}/{(isDebug ? "Debug.iToolConfig" : "Release.iToolConfig")}";
        }



        public static string PublicIp
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_PublicIp))
                {
#if DEBUG
                    _PublicIp = "127.0.0.1";
#else
                    _PublicIp = Utils.Excuter("cat /var/iTool/publicip.s");
#endif

                    if (string.IsNullOrWhiteSpace(_PublicIp))
                    {
                        _PublicIp = Utils.Excuter("curl www.icanhazip.com > /var/iTool/publicip.s");
                    }

                    if (!string.IsNullOrWhiteSpace(_PublicIp))
                    {
                        _PublicIp = _PublicIp.Trim().TrimEnd('\r').TrimEnd('\n').TrimEnd('\r');
                    }
                }

                return _PublicIp;
            }
        }

        public static bool IsSetteriToolConfigure { get; private set; }
        public static iToolConfigure iToolConfigure 
        {
            get 
            {
                if (File.Exists(_iToolConfigurePath))
                {
                    var content = File.ReadAllText(_iToolConfigurePath);
                    var configure = JsonConvert.DeserializeObject<iToolConfigure>(content);
                    return configure;
                }

                return default;
            }

            set 
            {
                if (value == null || value.AcceptQueueCount == 0)
                {
                    return;
                }

                value.Ip = PublicIp;

                if (File.Exists(_iToolConfigurePath))
                {
                    var oldContent = File.ReadAllText(_iToolConfigurePath);
                    var content = JsonConvert.SerializeObject(value);

                    if (content != oldContent)
                    {
                        File.WriteAllText(_iToolConfigurePath, content);
                    }
                }
                else
                {
                    File.Create(_iToolConfigurePath).Close();
                    File.WriteAllText(_iToolConfigurePath, JsonConvert.SerializeObject(value));
                }
                IsSetteriToolConfigure = true;
            }
        }

        public static string GatewayUrl
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_GatewayUrl))
                {
                    _GatewayUrl = Utils.Excuter("cat /var/iTool/gateway.s").Trim().TrimEnd('\r').TrimEnd('\n').TrimEnd('\r');
                }

                return _GatewayUrl;
            }
        }


        public static string SeedString
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_SeedString))
                {
                    if (File.Exists(_SeedfileName))
                    {
                        _SeedString = File.ReadAllText(_SeedfileName);
                    }
                }

                return _SeedString;
            }

            set 
            {
                if (!string.IsNullOrWhiteSpace(value))
                {
                    File.Create(_SeedfileName).Close();
                    File.WriteAllText(_SeedfileName, value);
                    _SeedString = value;
                }
            }
        }
    }
}
